<template>
  <div class="app-container">
    <el-button v-if="checkPermission(permissionRoles.item_recovery_add)" type="primary" @click="handleAdd">添加</el-button>

    <el-table v-loading="listLoading" :data="list" class="mt-15" height="720" border>
      <el-table-column label="ID编号" prop="id" align="center"></el-table-column>
      <el-table-column label="服务器" prop="sid" align="center"></el-table-column>
      <el-table-column label="角色ID" prop="actor_id" align="center"></el-table-column>
      <el-table-column label="发放物品" prop="item_name" align="center"></el-table-column>
      <el-table-column label="发放数量" prop="num" align="center"></el-table-column>
      <el-table-column label="发放时间" prop="create_dt" align="center"></el-table-column>
      <el-table-column label="操作人" prop="create_user" align="center"></el-table-column>

    </el-table>
    <el-dialog :visible.sync="dialogVisible" :title="textMap[dialogType]" :close-on-click-modal="false">
      <el-form ref="dataForm" :model="dataForm" :rules="rules" label-width="100px" label-position="left">
        <el-form-item label="服务器" prop="sid">
          <el-select v-model="dataForm.sid" class="defaut-input" filterable clearable>
            <el-option
              v-for="(item, index) in servers"
              :key="index"
              :label="item.name + '(' + item.sid + ')'"
              :value="item.sid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="角色ID" prop="actorId">
          <el-input v-model="dataForm.actorId" class="defaut-input" placeholder="请输入角色ID" />
        </el-form-item>
        <el-form-item label="发放道具">
          <el-row>
            <el-col :md="6">
              <el-select v-model="dataForm.itemId" clearable filterable placeholder="请选择物品">
                <el-option
                  v-for="p in products"
                  :key="p.id"
                  :label="p.desc"
                  :value="p.id"
                />
              </el-select>
            </el-col>
            <el-col :md="4"><el-input v-model="dataForm.num" placeholder="数量" /></el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <div class="tr">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmSubmit()">确定</el-button>
      </div>
    </el-dialog>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>
<script>
import { getItemRecoveryList, handleItemRecovery } from '@/api/gm'
import Pagination from '@/components/Pagination'
import checkPermission from '@/utils/permission'
import { getServerList } from '@/api/ops'
import { getItemConfig } from '@/api/config'

const defaultItem = {
  id: '',
  sid: '',
  actorId: '',
  itemId: '',
  num: ''
}
export default {
  name: 'DeviceCode',
  components: { Pagination },
  data() {
    return {
      permissionRoles: this.$store.state.user.permissionRoles,
      listLoading: false,
      dataForm: Object.assign({}, defaultItem),
      rules: {
      },
      listQuery: {
        page: 1,
        limit: 200
      },
      servers: [],
      products: [],
      total: 0,
      list: [],
      dialogVisible: false,
      dialogType: 'new',
      textMap: {
        new: '新增'
      }
    }
  },
  computed: {
  },
  created() {
    this.getList()
  },
  methods: {
    checkPermission,
    async getList() {
      this.listLoading = true
      getItemRecoveryList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        this.deviceSwitch = response.data.switch
        this.listLoading = false
      })
    },
    async getServerList() {
      const res = await getServerList()
      this.servers = res.data.items
    },
    async getItemConfig() {
      const res = await getItemConfig()
      this.products = res.data.items
    },
    handleAdd() {
      this.getServerList()
      this.getItemConfig()
      this.dataForm = Object.assign({}, defaultItem)
      this.dialogType = 'new'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    confirmSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          handleItemRecovery(this.dataForm).then(response => {
            if (response.status) {
              this.dialogVisible = false
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.getList()
            } else {
              this.message.error(response.message)
            }
          })
        }
      })
    }
  }
}
</script>
